package org.javarosa.xform.mesh4j.extensions;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

import org.javarosa.core.JavaRosaServiceProvider;
import org.javarosa.core.model.FormDef;
import org.javarosa.core.model.storage.DataModelTreeMetaData;
import org.javarosa.core.model.storage.DataModelTreeRMSUtility;
import org.javarosa.core.model.storage.FormDefRMSUtility;
import org.javarosa.xform.parse.XFormParser;
import org.mesh4j.sync.validations.MeshException;

import de.enough.polish.io.StringReader;

public class FormDefUtils {
	
	public static void importFormDefFromURL(String url){		
		FormDef formDef = getFormDefFromURL(url);		
		saveFormDef(formDef);
	}
	
	public static FormDef getFormDefFromURL(String url) {
		FormDef formDef = null;
		
		HttpConnection hc = null;
		InputStream in = null;
		Reader reader = null;
		try {
			hc = (HttpConnection) Connector.open(url);
			hc.setRequestMethod(HttpConnection.GET);
			in = hc.openInputStream();
			reader = new InputStreamReader(in, "UTF-8");
			
			formDef = XFormParser.getFormDef(reader);
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new MeshException(e.getMessage());
		} finally {
			try {
				if (in != null){
					in.close();
				}
				if (hc != null){
					hc.close();
				}
				if(reader != null){
					reader.close();
				}
			} catch (IOException ioe) {
				ioe.printStackTrace();
			}
		}
		return formDef;
	}
		
	public static void importFormDefFromXml(String xml){
		
		FormDef formDef = getFormDefFromXml(xml);
		
		saveFormDef(formDef);
	}

	public static FormDef getFormDefFromXml(String xml) {
		FormDef formDef = null;
		
		Reader reader = null;
		try {
			reader = new StringReader(xml);
			
			formDef = XFormParser.getFormDef(reader);
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new MeshException(e.getMessage());
		} finally {
			try {
				if(reader != null){
					reader.close();
				}
			} catch (IOException ioe) {
				ioe.printStackTrace();
			}
		}
		return formDef;
	}

	private static FormDefRMSUtility getFormDefRMSUtility() {
		return (FormDefRMSUtility) JavaRosaServiceProvider.instance().getStorageManager().getRMSStorageProvider().getUtility(FormDefRMSUtility.getUtilityName());
	}
	private static DataModelTreeRMSUtility getDataModelTreeRMSUtility() {
		return (DataModelTreeRMSUtility) JavaRosaServiceProvider.instance().getStorageManager().getRMSStorageProvider().getUtility(DataModelTreeRMSUtility.getUtilityName());
	}
	
	private static void saveFormDef(FormDef formDef) {
		FormDefRMSUtility rmsStorage = getFormDefRMSUtility();
		
		int recordId = rmsStorage.getIDfromName(formDef.getTitle());
		if(recordId == -1){
			rmsStorage.writeToRMS(formDef);
		} else {
			rmsStorage.updateToRMS(recordId, formDef);
		}
	}
	
	public static String getFormNameFromDataModelTreeID(int recordId){
		try{
			DataModelTreeMetaData metaData = new DataModelTreeMetaData();
			getDataModelTreeRMSUtility().retrieveMetaDataFromRMS(recordId, metaData);
			int formId = metaData.getFormIdReference();
			
			FormDef formDef = new FormDef();
			getFormDefRMSUtility().retrieveFromRMS(formId, formDef);
			return formDef.getTitle();
		}catch (Exception e) {
			e.printStackTrace();
			throw new MeshException(e.getMessage());
		}
	}
}
